package com.polaris.live.ui.set

import android.os.Bundle
import android.view.View
import android.widget.TextView
import com.blankj.utilcode.util.ColorUtils
import com.bumptech.glide.Glide
import com.lxj.xpopup.XPopup
import com.polaris.live.R
import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.AppConst
import com.polaris.live.common.ext.setOnSingleClickListener
import com.polaris.live.common.mvvm.activity.showLoading
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.ActivityAccountBindBinding
import com.polaris.live.dialog.DeleteAccountDialog
import com.polaris.live.translation.R.string
import com.polaris.live.ui.login.BaseGoogleLoginActivity
import com.polaris.live.utils.AppConfigUtils
import com.polaris.live.utils.UserUtils
import com.polaris.live.utils.image.loadAvatar
import com.polaris.live.utils.route.RoutePath
import com.polaris.live.utils.route.Router
import com.polaris.live.viewmodel.SetViewModel

/**
 * AccountBindActivity
 *
 * @author Created by 长空 on 2023/10/30/004 16:48
 */
class AccountBindActivity : BaseGoogleLoginActivity<SetViewModel, ActivityAccountBindBinding>() {

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        val user = UserManager.getUser()

        Glide.with(this)
            .loadAvatar(user.avatar)
            .into(mBinding.ivAvatar)

        mBinding.tvName.text = user.nickname
        mBinding.tvId.text = "UID:${user.userNumber.toString()}"

        if (AppConfigUtils.isShowPhoneButton()) {
            mBinding.csPhone.visibility = View.VISIBLE
        }

        initListener()
        initObserver()

        mViewModel.getThirdBind()
    }

    private fun initListener() {
        mBinding.tvDeleteAccount.setOnSingleClickListener {
            XPopup.Builder(this)
                .isDestroyOnDismiss(true)
                .asCustom(
                    DeleteAccountDialog(
                        this,
                        onConfirmCallback = {
                            mViewModel.cancelAccount()
                        }
                    )
                ).show()
        }
        mBinding.csPhone.setOnSingleClickListener {
            if (!mBinding.tvPhoneBind.isSelected) {
                return@setOnSingleClickListener
            }

            Router.instance.setPath(RoutePath.BIND_PHONE_ACTIVITY).navigation(this)
        }
        mBinding.csGmail.setOnSingleClickListener {
            if (!mBinding.tvGmailBind.isSelected) {
                return@setOnSingleClickListener
            }

            Router.instance.setPath(RoutePath.BIND_GMAIL_ACTIVITY).navigation(this)
        }
        mBinding.csGoogle.setOnSingleClickListener {
            if (!mBinding.tvGoogleBind.isSelected) {
                return@setOnSingleClickListener
            }

            showLoading(string.google_bindding, cancelTime = 5000)

            launchGoogle()
        }
    }

    private fun initObserver() {
        mViewModel.logoutLiveData.observe(this) {
            //退出登录不用管接口是不是调用成功
            UserUtils.logout()
        }

        mViewModel.terminateLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mViewModel.logout()
            }, {
                ToastGravity.showGravity(it.message)
            })
        }
        mViewModel.thirdBindLiveData.observe(this) { state ->
            parseState(state, { list ->
                val boundGoogle = list.find { it.userBindingType == AppConst.UserLoginType.GOOGLE } != null
                val boundPhone = list.find { it.userBindingType == AppConst.UserLoginType.PHONE } != null
                val boundGmail = list.find { it.userBindingType == AppConst.UserLoginType.GMAIL } != null

                mBinding.tvGoogleBind.visibility = View.VISIBLE
                mBinding.tvPhoneBind.visibility = View.VISIBLE
                mBinding.tvGmailBind.visibility = View.VISIBLE

                if (boundGoogle) {
                    updateViewBound(mBinding.tvGoogleBind)
                } else {
                    updateViewUnbound(mBinding.tvGoogleBind)
                }

                if (boundPhone) {
                    mBinding.csPhone.visibility = View.VISIBLE

                    updatePhoneBound(mBinding.tvPhoneBind)
                } else {
                    updatePhoneUnBound(mBinding.tvPhoneBind)
                }

                if (boundGmail) {
                    updateGmailBound(mBinding.tvGmailBind)
                } else {
                    updateGmailUnBound(mBinding.tvGmailBind)
                }
            })
        }
        mViewModel.googleBindLiveData.observe(this) { state ->
            dismissLoading()
            parseState(state, {
                updateViewBound(mBinding.tvGoogleBind)
            }, {
                ToastGravity.showGravity(it.message)
            })
        }
    }

    override fun googleLoginSuccess(id: String, token: String) {
        mViewModel.googleBind(id, token)
    }

    private fun updateViewUnbound(view: TextView) {
        mBinding.tvGoogleBind.isSelected = true
        view.text = getString(string.bind)
        view.setTextColor(ColorUtils.getColor(R.color.fe1849))
    }

    private fun updateViewBound(view: TextView) {
        mBinding.tvGoogleBind.isSelected = false
        view.text = getString(string.have_bind)
        view.setTextColor(ColorUtils.getColor(R.color.a7a8ad))
    }

    private fun updatePhoneUnBound(view: TextView) {
        mBinding.tvPhoneBind.isSelected = true
        view.text = getString(string.bind)
        view.setTextColor(ColorUtils.getColor(R.color.fe1849))
    }

    private fun updatePhoneBound(view: TextView) {
        mBinding.tvPhoneBind.isSelected = false
        view.text = getString(string.have_bind)
        view.setTextColor(ColorUtils.getColor(R.color.a7a8ad))
    }

    private fun updateGmailUnBound(view: TextView) {
        mBinding.tvGmailBind.isSelected = true
        view.text = getString(string.bind)
        view.setTextColor(ColorUtils.getColor(R.color.fe1849))
    }

    private fun updateGmailBound(view: TextView) {
        mBinding.tvGmailBind.isSelected = false
        view.text = getString(string.have_bind)
        view.setTextColor(ColorUtils.getColor(R.color.a7a8ad))
    }

    override fun onRestart() {
        super.onRestart()
        reStartActivity()
    }

    private fun reStartActivity() {
        mViewModel.getThirdBind()
    }
}